package com.imu.purse.modules.sms;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.foxinmy.weixin4j.http.ContentType;
import com.foxinmy.weixin4j.http.HttpClient;
import com.foxinmy.weixin4j.http.HttpClientException;
import com.foxinmy.weixin4j.http.HttpHeaders;
import com.foxinmy.weixin4j.http.HttpMethod;
import com.foxinmy.weixin4j.http.HttpRequest;
import com.foxinmy.weixin4j.http.HttpResponse;
import com.foxinmy.weixin4j.http.HttpStatus;
import com.foxinmy.weixin4j.http.entity.FormUrlEntity;
import com.foxinmy.weixin4j.http.entity.StringEntity;
import com.foxinmy.weixin4j.http.factory.HttpClientFactory;
import com.foxinmy.weixin4j.util.StringUtil;
import com.imu.purse.modules.sms.exception.SMSException;

/**
 * @className BaseApi
 * @author shaoguanlee
 * @date 2016/09/23
 * @since JDK 1.6
 * @see
 */
public class BaseApi
{
	private static final Logger logger = LoggerFactory.getLogger(BaseApi.class);
	protected static final HttpClient httpClient = HttpClientFactory.getInstance();

	public static String get(String url, Map<String, String> parameters) throws SMSException 
	{
		StringBuilder buf = new StringBuilder(url);
		if (parameters != null && !parameters.isEmpty())
		{
			if (url.indexOf("?") < 0)
			{
				buf.append("?");
			}
			else
			{
				buf.append("&");
			}
			
			buf.append(FormUrlEntity.formatParameters(parameters));
		}
		
		HttpRequest request = new HttpRequest(HttpMethod.GET, buf.toString());
		return execute(request, null);
	}

	public static String post(String url, String body) throws SMSException 
	{
		HttpRequest request = new HttpRequest(HttpMethod.POST, url);
		return execute(request, body);
	}

	public static String delete(String url, Map<String, String> parameters) throws SMSException
	{
		StringBuilder buf = new StringBuilder(url);
		if (parameters != null && !parameters.isEmpty()) 
		{
			if (url.indexOf("?") < 0)
			{
				buf.append("?");
			} 
			else
			{
				buf.append("&");
			}
			
			buf.append(FormUrlEntity.formatParameters(parameters));
		}
		
		HttpRequest request = new HttpRequest(HttpMethod.DELETE, buf.toString());
		return execute(request, null);
	}

	public static String put(String url, Map<String, String> parameters, String body) throws SMSException
	{
		StringBuilder buf = new StringBuilder(url);
		if (parameters != null && !parameters.isEmpty()) 
		{
			if (url.indexOf("?") < 0) 
			{
				buf.append("?");
			} 
			else
			{
				buf.append("&");
			}
			
			buf.append(FormUrlEntity.formatParameters(parameters));
		}
		
		HttpRequest request = new HttpRequest(HttpMethod.PUT, buf.toString());
		return execute(request, body);
	}

	public static String execute(HttpRequest request, String body) throws SMSException
	{
		if (request.getHeaders() == null) 
		{
			request.setHeaders(new HttpHeaders());
		}
		request.getHeaders().add("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
		
		if (StringUtil.isNotBlank(body)) 
		{
			request.setEntity(new StringEntity(body, ContentType.APPLICATION_JSON));
		}
		
		try 
		{
			logger.info("http request >> " + request.getMethod() + " " + request.getURI().toString() + (StringUtil.isNotBlank(body) ? ":" + body : ""));
			HttpResponse response = httpClient.execute(request);
			logger.info("http response << " + response.getProtocol() + response.getStatus());
			
			if (response.getStatus().getStatusCode() != HttpStatus.SC_OK)
			{
				throw new SMSException("execute fait....");
			}
			
			return StringUtil.newStringUtf8(response.getContent());
		} 
		catch (HttpClientException e) 
		{
			throw new SMSException(e.getMessage());
		}
	}
}
